package com.tudou.top2.model
{
	import asset.Loading;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	
	public class PictsDataModel
	{
		[Embed(source="../asset/blackBar.png")]
		private var blackBar:Class;
		
		[Embed(source="../asset/playBtn.png")]
		private var playBtn:Class;

		public var smallBoxes:Array;

		public var links:Array;
		public var bigTitles:Array;
		public var bigTextColors:Array;
		public var bigPicts:Array;
		
		private var currPict:int;
		public var arrayLength:uint;
		
		public function PictsDataModel()
		{
			links = new Array();
			bigTitles = new Array();
			bigTextColors = new Array();
			
			smallBoxes = new Array();
			var i:int = 0;
			for(i=0; i<4; i++)
			{
				smallBoxes[i] = new SmallBox();
			}
			
			bigPicts = new Array();
			for(i=0; i<10; i++)
			{
				bigPicts[i] = new Sprite();
				//加入进度条
				var loadingBar:Loading = new Loading();
				bigPicts[i].addChild(loadingBar);
				loadingBar.x = KiliTop2.flashWidth/2 + 75; 
				loadingBar.y = KiliTop2.flashHeight/2;
			}
		}
		public function addBigPict(index:uint, disp:DisplayObject):void
		{
			if(disp == null) return ;
			//图片
			var sp:Sprite = bigPicts[index];
			disp.x = disp.y = 0;
			sp.addChild(disp);
			sp.buttonMode = true;
			sp.useHandCursor = true;
//调试时显示出Loading条			
//			disp.visible = false;
			var childSprite:Sprite = new Sprite();
			childSprite.name = "title"
			childSprite.buttonMode = true;
			childSprite.useHandCursor = true;
			childSprite.alpha = 0;
			childSprite.scaleY = childSprite.scaleX = 1;
//画出Title的框			
//			childSprite.graphics.beginFill(0xff0000);
//			childSprite.graphics.drawRect(0, 0, 100, 100);
			//加入播放图片
			setPlayPict(childSprite);
			//产生一个Title
			var subTitle:DisplayObject = produceTitle(index);
			subTitle.x += 8;
			subTitle.y += 3.3;
			childSprite.addChild(subTitle);
			childSprite.x = 342;
			childSprite.y = 310;
			sp.addChild(childSprite);
			//点击跳转
			sp.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void
				{
					navigateToURL(new URLRequest(links[index]), "_blank");
				}
			);
			childSprite.cacheAsBitmap = true;
		}
		
		private function produceTitle(index:int):DisplayObject
		{
			//文字描述
			var tf:TextField = new TextField();
			tf.name = "text";
			if(bigTextColors[index]!=null)
			{
				tf.textColor = bigTextColors[index];
			}
			else
			{
				tf.textColor = 0xFFFFFF;
			}
			tf.alpha = 0;
			tf.border = false;
			tf.multiline = true;
			tf.selectable = false;
			tf.type = TextFieldType.DYNAMIC;
			tf.blendMode = BlendMode.NORMAL;
			tf.autoSize = TextFieldAutoSize.LEFT;
			if(bigTitles[index] == null)
			{
				tf.text = "";
			}
			else
			{
				tf.text = bigTitles[index];
			}
//以前的方法，读入到一个Bitmap中
			BitmapData		
			var bmpData:BitmapData = new BitmapData(278, 34, true, 0x00000000);
			bmpData.draw(tf);
			
			var bmp:Bitmap = new Bitmap(bmpData);
			return bmp;
		}
		public function addSmallPict(index:uint, disp:DisplayObject):void
		{
			var sp:Sprite = new Sprite();
			smallBoxes[index].smallPict = sp;	
					
			if(disp == null) return ;
			sp.addChild(disp);
		}
		
		public function get currentPict():int
		{
			return currPict;
		} 
	
		public function set currentPict(i:int):void
		{
			currPict = i;
		}
		
		private function setPlayPict(disCon:DisplayObjectContainer):void
		{
			var bar:Bitmap = new blackBar();
			var play:Bitmap = new playBtn();
			
			bar.alpha = 0.8;
			disCon.addChild(bar);
			disCon.addChild(play);
			
			play.x = 390;
			play.y = -17;
		}
		
	}
}